import os
import sys
import tempfile
import arcpy
import stormwaterutilities
from stormwaterutilities import addMessage

def PolylineRasterIntersect(lineFeatureClassPath, rasterPath, rasterField='Value', prefix=''):

    tempFolder = tempfile.gettempdir()
    logFile = os.path.join(tempFolder, "log.txt")

    args = ["PolylineRasterIntersect", lineFeatureClassPath, rasterPath, rasterField, prefix]
    addMessage("Executing PolylineRasterIntersect with arguments: " + str(args))

    stormwaterutilities.callGpTool(args)

class PolylineRasterIntersectTool(object):
    def __init__(self):
        """Intersect a line feature class with a raster and creates field in the line class showing length of the line
        within different raster classifications."""
        self.label = "Polyline Raster Intersection Tool"
        self.description = """Intersect a line feature class with a raster and creates field in the line class showing length of the line
                            within different raster classifications."""
        self.canRunInBackground = True

    def getParameterInfo(self):
        """Define parameter definitions"""
        param0 = arcpy.Parameter(
            displayName="Lines Feature Class",
            name="lines_fc",
            datatype="GPFeatureLayer",
            parameterType="Required",
            direction="Input")

        param0.filter.list = ['LINE']

        param1 = arcpy.Parameter(
            displayName="Raster",
            name="raster",
            datatype="GPRasterLayer",
            parameterType="Required",
            direction="Input")

        param2 = arcpy.Parameter(
            displayName="Raster Field",
            name="rasterField",
            datatype="Field",
            parameterType="Required",
            direction="Input")

        param2.parameterDependencies =[param1.name]

        param3 = arcpy.Parameter(
            displayName="Prefix for output fields",
            name="prefix",
            datatype="GPString")

        param3.value = ""

        params = [param0, param1, param2, param3]
        return params

    def isLicensed(self):
        """Set whether tool is licensed to execute."""
        return True

    def updateParameters(self, parameters):
        """Modify the values and properties of parameters before internal
        validation is performed.  This method is called whenever a parameter
        has been changed."""
        return

    def updateMessages(self, parameters):
        """Modify the messages created by internal validation for each tool
        parameter.  This method is called after internal validation."""
        return

    def execute(self, parameters, messages):
        """The source code of the tool."""
        stormwaterutilities.overridden_log_path = os.path.expanduser(r'~\documents\temp\Swsp.Toolbox.log')
        addMessage('Logging to: ' + stormwaterutilities.overridden_log_path)

        (lines_fc, raster, rasterField, prefix) = stormwaterutilities.convertParametersToFullPathStrings(parameters)
        PolylineRasterIntersect(lines_fc, raster, rasterField, prefix)
        return

"""
if __name__ == "__main__":
    print("accumulatetonetwork.py is being run directly with args:")

    print(sys.argv)

    AccumulateToNetwork(sys.argv)
else:
    #print("accumulatetonetwork.py is being imported into another module:" + __name__)

    #print(sys.argv)
"""
